﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class MessagesList : BasePage
    {
        private MessageLogic messageLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitMessageList(1);
            }
        }

        /// <summary>
        /// 加载留言列表
        /// </summary>
        /// <param name="pageIndex"></param>
        private void InitMessageList(int pageIndex)
        {
            this.gvMessages.PageIndex = pageIndex;
            List<Message> messageList = messageLogic.GetList().OrderByDescending(p => p.Id).ToList();
            this.gvMessages.DataSource = messageList;
            this.gvMessages.DataBind();
        }

        /// <summary>
        /// 获取要显示的正确 页码数
        /// </summary>
        /// <param name="oldPageIndex"></param>
        /// <returns></returns>
        private int GetShowPageIndex(int oldPageIndex)
        {
            return this.gvMessages.PageCount < oldPageIndex ? this.gvMessages.PageCount : oldPageIndex;
        }

        protected override void InitLogic()
        {
            messageLogic = new MessageLogic();
        }

        protected override void DisposeLogic()
        {
            if (messageLogic != null)
            {
                messageLogic.Dispose();
                messageLogic = null;
            }
        }

        protected void gvMessages_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            InitMessageList(e.NewPageIndex);
        }

        protected void gvMessages_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int pageIndex = this.gvMessages.PageIndex;

                long id = long.Parse(e.Keys[0].ToString());

                Message message = messageLogic.GetById(id);
                if (message != null)
                {
                    messageLogic.Delete(message);
                }

                AddScript("alert('删除留言成功！');");

                InitMessageList(GetShowPageIndex(pageIndex));

            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('删除留言失败！');");
            }
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string sid = (sender as LinkButton).CommandArgument;
                long id = 0;
                if (long.TryParse(sid, out id))
                {
                    Message message = messageLogic.GetById(id);
                    if (message != null)
                    {
                        this.txtMsgTitle.Text = message.MsgTitle;
                        this.txtMsgContent.Text = message.MsgContent;
                        this.txtMsgRe.Text = message.MsgRe;
                        this.SavePushButton.CommandArgument = sid;
                    }
                    else
                    {
                        AddScript("alert('获取留言信息失败!');");
                    }
                }
                else
                {
                    AddScript("alert('获取留言信息失败!');");
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('获取留言信息失败!');");
            }
        }

        protected void SavePushButton_Click(object sender, EventArgs e)
        {
            try
            {
                int pageIndex = this.gvMessages.PageIndex;
                long id = 0;

                if (long.TryParse(SavePushButton.CommandArgument, out id))
                {
                    Message msg = messageLogic.GetById(id);
                    msg.MsgRe = txtMsgRe.Text;
                    msg.MsgReDate = DateTime.Now;
                    msg.MsgHasRe = true;

                    messageLogic.Save(msg);

                    InitMessageList(pageIndex);
                    ClearControlText(txtMsgTitle, txtMsgContent, txtMsgRe);
                    this.SavePushButton.CommandArgument = string.Empty;
                    AddScript("回复成功。");
                }
                else
                {
                    ClearControlText(txtMsgTitle, txtMsgContent, txtMsgRe);
                    this.SavePushButton.CommandArgument = string.Empty;
                    AddScript("alert('回复失败，请重试！')");
                }
            }
            catch (Exception ex)
            {
                ClearControlText(txtMsgTitle, txtMsgContent, txtMsgRe);
                this.SavePushButton.CommandArgument = string.Empty;
                Loger.LogErr(ex);
                AddScript("alert('回复失败，请重试！')");
            }
        }
    }
}